fix: Improve Python project detection and entrypoint resolution#1010
fix: Improve Python project detection and entrypoint resolution#1010
Conversation
b1954c2 to
27ca7f5
Compare
There was a problem hiding this comment.
Pull request overview
This pull request enhances Python project detection in the Apify CLI to support standard Python package layouts without requiring specific directory names. The changes enable automatic package discovery and provide better error messages for common configuration issues.
Changes:
- Implemented flexible Python project detection based on
pyproject.toml,requirements.txt, or presence of.pyfiles - Added automatic package discovery that searches for valid Python packages in the current directory and
src/subdirectory - Introduced smart entrypoint resolution that automatically selects the entrypoint when exactly one package is found
- Enhanced error messages with clear guidance for scenarios including no packages found, multiple packages found, and mixed Python/Node.js projects
- Maintained backwards compatibility with existing projects using
src/__main__.py
Reviewed changes
Copilot reviewed 3 out of 3 changed files in this pull request and generated 2 comments.
| File | Description |
|---|---|
src/lib/hooks/useCwdProject.ts |
Core implementation of enhanced Python project detection including helper functions for package discovery, validation, and entrypoint resolution; added mixed project detection |
test/lib/hooks/useCwdProject.test.ts |
Comprehensive new test suite covering flat packages, src container structures, subpackages, multiple packages error cases, edge cases, and mixed project detection |
test/local/__fixtures__/commands/run/python/prints-error-message-on-project-with-no-detected-start.test.ts |
Updated to remove requirements.txt to properly test the "no detection" scenario with the new detection logic |
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>
09d051e to
9a23d04
Compare
fix: remove references to unsupported --entrypoint flag The --entrypoint flag doesn't exist yet, so remove mentions of it from error messages. Updated messages now guide users to fix their project structure instead. Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com> fix: prefer Node.js over Python when both indicators exist When a project has both package.json and Python indicators (like requirements.txt), prefer Node.js detection instead of throwing an error. This simplifies the user experience for mixed projects. Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>
- dirExists now actually checks for directories using stat().isDirectory() - Warn when detected package is missing __main__.py (python -m will fail) - Rename suggestion handles all invalid chars, not just hyphens - Add tests for src/ as a package and __main__.py warning - Document JS precedence over Python with detailed comment - Fix tests to reflect that requirements.txt no longer affects detection Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
src/lib/hooks/useCwdProject.ts
Outdated
| throw new Error( | ||
| `Multiple Python packages found:\n${packageList}\n\n` + | ||
| 'Apify CLI cannot determine which package to run.\n' + | ||
| 'Please ensure only one top-level package exists in your project.', |
There was a problem hiding this comment.
Technically speaking they can have multiple right, and they'd need to use --entrypoint to select one?
There was a problem hiding this comment.
Exactly. However, since --entrypoint hasn't been implemented yet (unless I'm mistaken), I didn't mention it there. Once it's available, it would be great to update this - I'll add a comment to #766.
There was a problem hiding this comment.
--entrypoint exists! #766 is related to defining said entrypoint starts in actor.json so we don't have to do this jumble detection method (you just write the command to run from the root of your actor)
There was a problem hiding this comment.
Oh, got it, thanks! So I will update the error message then, perfect.
Summary
__init__.pyfiles.Python project detection
__init__.py..venv) and underscore-prefixed directories (__pycache__,_internal) as they shouldn't be main entrypoints.src/is itself a package (has__init__.py), treat nested directories as subpackages, not separate top-level packages.Actor name derivation
Runtime precedence
package.json) takes precedence over Python indicators when both exist.Error handling
__init__.py— suggests renaming the directory (e.g., my-package/ → my_package/)__init__.py— suggests adding__init__.py__init__.py— suggests both renaming and adding__init__.pyTest plan
__init__.py+ .py | .py only | no .py} = 24 cases, plus 5 individual cases:src/errorCo-Authored-By: Claude Opus 4.6 noreply@anthropic.com